package com.xxgc.springboot.controller.exception;

import com.xxgc.springboot.controller.exception.custom.*;
import com.xxgc.springboot.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: springboot
 * @description: 统一异常处理
 * @author: Mr.abel(ShiJiaYi)
 * @create: 2022-09-21 16:32
 **/
@Slf4j
@ControllerAdvice
@ResponseBody
public class GlobalException {

    @Value("${spring.profiles.active}")
    private String active;
    @Value("${system.bug.msg}")
    private String system;
    //哪个环境进行语句替换
    @Value("${system.bug.grade}")
    private String myActive;
    /**
     * 创建定时器异常
     * @param e
     * @return6
     */
    @ExceptionHandler(value = {QuartzTimeException.class})
    public Result quartzTimeException(QuartzTimeException e){
        log.error(e.toString());
        String msg = active.equals(myActive) ? system : e.getDefaultMessage();
        return Result.error(msg,e.getCode());
    }

    @ExceptionHandler(value = NotQuartzClassFoundException.class)
    public Result notQuartzClassFoundException(NotQuartzClassFoundException e){
        log.error(e.toString());
        String msg = active.equals(myActive) ? system : e.getDefaultMessage();
        return Result.error(msg,e.getCode());
    }
    //token异常
    @ExceptionHandler(value = TokenInvalidException.class)
    public Result tokenInvalidException(TokenInvalidException e){
        log.error(e.toString());
        String msg = active.equals(myActive) ? system : e.getDefaultMessage();
        return Result.error(msg,e.getCode());
    }

    @ExceptionHandler(value = TokenNullException.class)
    public Result tokenNullException(TokenNullException e){
        log.error(e.toString());
        String msg = active.equals(myActive) ? system : e.getDefaultMessage();
        return Result.error(msg,e.getCode());
    }
    //无权限
    @ExceptionHandler(value = NoPermissionException.class)
    public Result noPermissionException(NoPermissionException e){
        log.error(e.toString());
        String msg = active.equals(myActive) ? system : e.getDefaultMessage();
        return Result.error(403,msg,e.getCode());
    }

    //登录找不到用户
    @ExceptionHandler(value = AuthenticationException.class)
    public Result authenticationException(AuthenticationException e){
        log.error(e.toString());
        String msg = active.equals(myActive) ? system : "账户被锁定";
        return Result.error(msg,MessageException.MSG_HEAD+"Login-01");
    }
    //账号找到了，密码错了
    @ExceptionHandler(value = IncorrectCredentialsException.class)
    public Result incorrectCredentialsException(IncorrectCredentialsException e){
        log.error(e.toString());
        String msg = active.equals(myActive) ? system : "用户名或密码错误";
        return Result.error(msg,MessageException.MSG_HEAD+"Login-02");
    }
    //账号都没找到
    @ExceptionHandler(value = UnknownAccountException.class)
    public Result unknownAccountException(UnknownAccountException e){
        log.error(e.toString());
        String msg = active.equals(myActive) ? system : "用户名或密码错误";
        return Result.error(msg,MessageException.MSG_HEAD+"Login-03");
    }
    //无权限的异常
    @ExceptionHandler(value = UnauthorizedException.class)
    public Result unauthorizedException(UnauthorizedException e){
        log.error(e.toString());
        String msg = active.equals(myActive) ? system : "账号无权限";
        return Result.error(msg,MessageException.MSG_HEAD+"Login-04");
    }


}
